﻿#if DebugTest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Runtime.Serialization;
using Ivan.Dm.ChromeRemoteDebugger.Protocol;

namespace Ivan.Dm.ChromeRemoteDebugger.Tests.Protocol {
    [TestFixture]
    public class TestJsonHelper {
        [DataContract]
        class BaseTestClass {
            [DataMember(Name = "integer_property")]
            public int IntegerProperty { get; set; }
        }

        [DataContract]
        class TestClass : BaseTestClass {
            [DataMember(Name = "string_array_property")]
            public string[] StringArrayProperty { get; set; }

            [DataMember(Name = "list_of_arrays_property")]
            public List<object[]> ListOfArraysProperty { get; set; }
        }

        [Test]
        public void Serialize() {
            string expectedJson = "{\"integer_property\":3," +
                                  "\"list_of_arrays_property\":[[1,\"value\"]]," +
                                  "\"string_array_property\":[\"testStr1\",\"testStr2\"]}";
            var objectInstance = new TestClass() {
                IntegerProperty = 3,
                StringArrayProperty = new string[2],
                ListOfArraysProperty = new List<object[]>()
            };
            objectInstance.StringArrayProperty[0] = "testStr1";
            objectInstance.StringArrayProperty[1] = "testStr2";
            objectInstance.ListOfArraysProperty.Add(new object[] { 1, "value" });
            string serializedObject = JsonHelper.Serialize<TestClass>(objectInstance);
            Assert.AreEqual(expectedJson, serializedObject);
        }

        [Test]
        public void Deserialize() {
            string serializedObject = "{\"integer_property\":3," +
                                      "\"list_of_arrays_property\":[[1,\"value\"]]," +
                                      "\"string_array_property\":[\"testStr1\",\"testStr2\"]}";
            TestClass objectInstance = JsonHelper.Deserialize<TestClass>(serializedObject);
            Assert.AreEqual(3, objectInstance.IntegerProperty);
            Assert.AreEqual("testStr1", objectInstance.StringArrayProperty[0]);
            Assert.AreEqual("testStr2", objectInstance.StringArrayProperty[1]);
            Assert.AreEqual(1, objectInstance.ListOfArraysProperty[0][0]);
            Assert.AreEqual("value", objectInstance.ListOfArraysProperty[0][1]);
        }

        [Test]
        public void Deserialize_ToBaseClass() {
            string serializedObject = "{\"integer_property\":3," +
                                      "\"list_of_arrays_property\":[[1,\"value\"]]," +
                                      "\"string_array_property\":[\"testStr1\",\"testStr2\"]}";
            BaseTestClass objectInstance = JsonHelper.Deserialize<BaseTestClass>(serializedObject);
            Assert.AreEqual(3, objectInstance.IntegerProperty);
        }
    }
}
#endif